<template>

  <div class="admin-body">
    <el-row>
      <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item v-if="account">平台统计</el-breadcrumb-item>
        <el-breadcrumb-item>{{(typeApply==1||typeApply==2)?'城市代理商管理':'推广代理商管理'}}</el-breadcrumb-item>
        <el-breadcrumb-item >{{(typeApply==1||typeApply==3)?'个人代理商管理':'组织代理商管理'}}</el-breadcrumb-item>
      </el-breadcrumb>
    </el-row>
    <el-row style="padding: 15px">
      <el-row class="main">
        <!--顶部-->
        <el-row style="padding-bottom: 10px;text-align: left">
            <el-input placeholder="名称或手机号" v-model="selectkey" class="input-with-select" style="width: 200px">
            </el-input>
            <el-button type="primary" @click="getStart()">搜索</el-button>
          <el-select v-model="statekey" placeholder="状态筛选" @change="getStart()">
            <el-option label="完结" :value="1"></el-option>
            <el-option label="未完结" :value="2"></el-option>
            <el-option label="默认" :value="3"></el-option>
          </el-select>
          <el-date-picker
            v-model="dateValue"
            @change="getStart()"
            value-format="timestamp"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>


          <div style="float: right">
            <el-button type="success" icon="el-icon-edit-outline" @click="exportExcel()">导出月收入统计</el-button>
            <el-button type="success" icon="el-icon-edit-outline" @click="exportExcelCZ()">导出扫码并充值用户详情</el-button>
            <el-button type="primary" style="margin-left: 10px" @click="goHref('agentApply')" v-if="!account">申请列表</el-button>
          </div>
        </el-row>

        <!--表格-->
        <el-table
          border
          @selection-change="handleSelectionChange"
          align="left"
          :data="tableData"
          style="width: 100%">
          <el-table-column
            prop="name"
            label="名称">
          </el-table-column>
          <el-table-column
            label="性别">
            <template slot-scope="scope">
              {{ scope.row.gender==1?'男':'女'}}
            </template>
          </el-table-column>
          <el-table-column
            prop="phone"
            label="手机号">
          </el-table-column>
          <el-table-column
            prop="address"
            label="所在地">
          </el-table-column>
          <el-table-column
            label="通过时间">
            <template slot-scope="scope">
              {{ toDate(scope.row.passtime) }}
            </template>
          </el-table-column>
          <el-table-column
            label="月收入统计">
            <template slot-scope="scope">
              <el-button  @click="goHrefS ('agentIncome','incomeData',{id:scope.row.id,apiM:'monthlyProfit',apiD:'dayIncomeDetails',apiS:'editEndState'})">查看</el-button>
            </template>
          </el-table-column>
          <el-table-column
            label="扫码用户详情">
            <template slot-scope="scope">
              <el-button  @click=" goHrefS ('agentCode','codeData',{id:scope.row.id,api:'scanRecord'})">查看</el-button>
            </template>
          </el-table-column>
          <el-table-column
            label="扫码并充值用户详情">
            <template slot-scope="scope">
              <el-button  @click=" goHrefS ('agentCharge','chargeData',{type:2,id:scope.row.id,api:'rechargeList',apiT:'statistics'})">查看</el-button>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            width="120">
            <template slot-scope="scope">
              <el-button @click="changeMes(scope.row.recharge_id,0)" size="small" type="text">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <!--分页-->
        <el-col :span="24" style="margin-top: 15px">
          <el-pagination
            style="text-align: right"
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="dataTotal">
          </el-pagination>
        </el-col>
      </el-row>
    </el-row>

    <excel-dao-chu ref="excel"   :my-data="excelData" :my-name="excelName"  :my-title-obj="excelTitleData"></excel-dao-chu>

  </div>
</template>

<script>
  import ExcelDaoChu from '../../components/ExcelDaoChu'

  export default {
    name: "AgentList",
    components:{
      ExcelDaoChu
    },
    mounted() {
      if (!this.$store.state.login.in) {
        this.$router.push({name: "login", params: {userId: 123}})
      }
      this.getStart()

    },
    data() {
      return {
        tableData: [],
        pageSize: 10,
        currentPage: 1,
        selectkey: '',
        dataTotal: 0,
        multipleSelection: [],
        formLabelWidth: '80px',
        dateValue:[
          (new Date(new Date(new Date().toLocaleDateString()).getTime())-1000 * 60 * 60 * 24*(new Date().getDate()-1)),
          new Date()
        ],
        excelData:[],
        excelTitleData:{},
        excelName:'',
        statekey:3

      }
    },
    computed: {
      typeApply(){
        return this.$store.state.login.typeApply
      },
      account(){
        return this.$store.state.login.account
      },
    },
    watch:{
      typeApply(){
         this.getStart()
      }
    },
    methods: {
      // 获取数据
      getStart() {
        console.log(this.dateValue)
        let thi = this
        const por = {
          page: thi.currentPage,
          pageSize: thi.pageSize,
          key: thi.selectkey,
          type:thi.typeApply,
          ent_state:thi.statekey,
          start_time:~~(thi.dateValue[0]/1000),
          over_time:~~(thi.dateValue[1]/1000)
        }
        thi.mySelectAll('agentList', por).then(function (value) {
          thi.tableData = value.data
          thi.dataTotal = value.Total
        })
      },
      // Excel导出
      exportExcel(){
        let thi= this
        const por={
          type:thi.typeApply,
          start_time:~~(thi.dateValue[0]/1000),
          over_time:~~(thi.dateValue[1]/1000)
        }
        thi.mySelect('exportDayIncomeDetails',por).then(function (data) {
          thi.excelData=data
          thi.excelTitleData={date:'日期',sum_money:'获得金额',sum_number:'推广人数',name:'推广人姓名',end_state:'完结状态'}
          thi.excelName='月收入统计'
          thi.$refs.excel.sureExcel()
        })
      },
      // Excel导出
      exportExcelCZ(){
        let thi= this
        const por={
          type:thi.typeApply,
          start_time:~~(thi.dateValue[0]/1000),
          over_time:~~(thi.dateValue[1]/1000)
        }
        thi.mySelect('exportScanAndRechargeList',por).then(function (data) {
          thi.excelData=data
          thi.excelTitleData={orderno:'订单号',price:'充值金额',pay_time:'充值时间',nickname:'用户昵称',phone:'用户手机号',sum_number:'推广总人数',sum_money:'充值总金额'}
          thi.excelName='扫码并充值用户信息'
          thi.$refs.excel.sureExcel()
        })
      },
      //状态改变提示
      changeMes(id, state) {
        var heeh = '该'
        var caozuo = '禁用'
        if (id.length > 1) {
          heeh = '这些'
        }
        if (state == 0) {
          caozuo = '删除'
        }
        let thi = this
        thi.$confirm('此操作将'+caozuo + heeh+'信息', '是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'error'
        }).then(() => {
          thi.submitState(id, state)
        }).catch(() => {
          thi.$message({
            type: 'info',
            message: '已取消'
          });
        });
      },
      //状态改变
      submitState(id, state) {
        let thi = this
        const por = {
          recharge_id: id,
          recharge_state: state
        }
        thi.mySubmit('rechargedelete', por).then(function (code) {
          if (code == 200) {
            thi.getStart()
          }
        })
      },
      //多选
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      //批量状态改变合并ID
      deletesel() {
        if (this.multipleSelection.length == 0) {
          this.$message({
            type: 'warning',
            message: '请选择要删除的分类!'
          });
          return
        } else {
          var ids = '';
          this.multipleSelection.forEach(function (avalue, aindex, aarr) {
            if (aindex == 0) {
              ids = avalue.recharge_id
            } else {
              ids = ids + ',' + avalue.recharge_id
            }
          });
          this.changeMes(ids, 0)
        }
      },
      //选择展示数量
      handleSizeChange(val) {
        this.pageSize = val
        this.getStart()
      },
      //跳转页面
      handleCurrentChange(val) {
        this.currentPage = val
        this.getStart()
      },
    }
  }
</script>

<style scoped>
  .admin-body {
    width: 100%;
  }
  .main {
    background-color: white;
    min-height: 500px;
    padding: 10px;
    border: #EDEDED 1px solid;
    border-radius: 5px;
  }
</style>
